In [1]:
import wfdb
import numpy as np
import matplotlib.pyplot as plt
import sys
Downloading an ECG from mimic III
In [2]:
folder = "p05/p050140"
waveform = "p050140-2188-07-26-05-51"
sig, fields = wfdb.srdsamp(waveform,pbdir='mimic3wdb/matched/'+folder)
In [3]:
print("signame: " + str(fields['signame']))
print("units: " + str(fields['units']))
print("fs: " + str(fields['fs']))
print("comments: " + str(fields['comments']))
print("fields: " + str(fields))
Lets check if the signal contains the signal II
In [4]:
signalII = None
try:
signalII = fields['signame'].index("II")
except ValueError:
print("List does not contain value")
if(signalII!=None):
print("List contain value ",signalII)
Obtain only the signal II from the ECG
In [5]:
array = sig[:, signalII]
array = np.nan_to_num(array)
Save the Frame Secuence
In [6]:
fs = fields['fs']
fs
Out[6]:
SubArray 400000
In [18]:
window = 100000
sampfrom = 0
sampto = window
subArray = array[sampfrom:sampto]
print(len(subArray))
print(subArray)
Out[18]:
In [8]:
from oct2py import octave
octave.addpath('/home/scidb/HeartRatePatterns/Matlab/')
octave.eval('pkg load signal')
In [9]:
%load_ext oct2py.ipython
In [10]:
%%octave -i subArray,fs -o R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot
[R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot] = peakdetect(subArray,fs,false);
In [17]:
Ri = R_i
Ramp = R_amp
Si = S_i
Samp = S_amp
Ti = T_i
Tamp = T_amp
Qi = Q_i
Qamp = Q_amp
In [22]:
sampfrom += window+1
sampto += window
subArray = array[sampfrom:sampto]
In [23]:
%%octave -i subArray,fs -o R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot
[R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot] = peakdetect(subArray,fs,false);
In [25]:
print("R_i",R_i)
print("Ri",Ri)
print("S_i",S_i)
print("R_i",T_i)
print("Q_i",Q_i)
print(heart_rate)
print(buffer_plot)
In [12]:
To= 5
subarray = buffer_plot[0][Q_i[0][0]:T_i[0][To]]
print(len(subarray))
subQ_i = Q_i[0][:To]
subQ_amp = Q_amp[0][:To]
subR_i = R_i[0][:To]
subR_amp = R_amp[0][:To]
subS_i = S_i[0][:To]
subS_amp = S_amp[0][:To]
subT_i = T_i[0][:To]
subT_amp = T_amp[0][:To]
In [13]:
fig_size = [12,9]
plt.rcParams["figure.figsize"] = fig_size
In [14]:
plt.plot(np.arange(Q_i[0][0],T_i[0][To]),np.array(subarray),c='lightgreen')
plt.scatter(subQ_i, subQ_amp,c='yellow')
plt.scatter(subR_i, subR_amp,c='blue')
plt.scatter(subS_i, subS_amp,c='red')
plt.scatter(subT_i, subT_amp,c='black')
plt.show()
In [29]:
size = 65
sq = S_i[0][:size]-Q_i[0][:size]
sq
Out[29]:
In [30]:
tq = T_i[0][:size]-Q_i[0][:size]
tq
Out[30]:
In [31]:
ts = T_i[0][:size]-S_i[0][:size]
ts
Out[31]:
In [32]:
heartRate = np.stack((sq, tq,ts), axis=-1)
In [33]:
from sklearn.cluster import KMeans
In [34]:
X = np.array(heartRate)
est = KMeans(n_clusters=10).fit(X)
In [35]:
print(est.labels_)
print(est.cluster_centers_)
In [36]:
import collections
In [42]:
labels, values = zip(*collections.Counter(est.labels_).items())
#labels, values = zip(*collections.Counter(patient).most_common(14))
indexes = np.arange(len(labels))
width = 0.5
fig_size = [12,9]
fig, ax = plt.subplots()
for i, v in enumerate(values):
ax.text(i-0.1,v+1, str(v), color='blue', fontweight='bold')
plt.rcParams["figure.figsize"] = fig_size
plt.bar(indexes, values, width)
plt.xticks(indexes + width * 0.01, labels)
plt.show()
In [ ]:
sampfrom = max(max(R_i[0]),max(S_i[0]),max(T_i[0]),max(Q_i[0]))
sampfrom
In [ ]:
sampfrom = 400000
sampto = 400000+sampfrom
folder = "p05/p050140"
waveform = "p050140-2188-07-26-05-51"
sig, fields = wfdb.srdsamp(waveform,pbdir='mimic3wdb/matched/'+folder,
sampfrom=sampfrom, sampto=sampto)
In [ ]:
array = np.nan_to_num(sig[:, signalII])
array = array
array
In [ ]:
%%octave -i array -o R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot
[R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot] = peakdetect(array,125,false);
In [ ]:
print(fs)
print("R_i",R_i)
print("S_i",S_i)
print("R_i",T_i)
print("Q_i",Q_i)
print(heart_rate)
print(buffer_plot)
In [ ]: